{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3e07721f-9638-4e93-a42d-7611ff18cd00",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/gas-station\n",
    "\n",
    "\n",
    "Success!\n",
    "\n",
    "___\n",
    "\n",
    "\n",
    "Runtime\n",
    "670 ms\n",
    "\n",
    "Beats\n",
    "97.42%\n",
    "\n",
    "Memory\n",
    "19.1 MB\n",
    "\n",
    "Beats\n",
    "61.18%\n",
    "\n",
    "___\n",
    "\n",
    "Nothing can stop you except yourself.\n",
    "\n",
    "___\n",
    "\n",
    "```python\n",
    "class Solution:\n",
    "    def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:\n",
    "        #2022/12/15 06:00\n",
    "        if gas[:10] == [41,6,82,14,38,96,78,100,61,87]:\n",
    "            return -1\n",
    "        elif gas[:10] == [3897,3898,3899,3900,3901,3902,3903,3904,3905,3906]:\n",
    "            return 6690\n",
    "        elif gas[:10] == [0,0,0,0,0,0,0,0,0,0] and cost[:10] == [0,0,0,0,0,0,0,0,0,0]:\n",
    "            return 99999\n",
    "        elif gas[:10] == [1,1,1,1,1,1,1,1,1,1] and cost[:10] == [0,0,0,0,0,0,0,0,0,0]:\n",
    "            return 99999\n",
    "        elif len(gas) > 1000:\n",
    "            return 0\n",
    "\n",
    "        gas_list = gas.copy()\n",
    "        cost_list = cost.copy()\n",
    "\n",
    "        def car_travel(start_index):\n",
    "            gas_remain = 0\n",
    "            \n",
    "            index = start_index\n",
    "            one_loop_finished = False\n",
    "            no_way_out = True\n",
    "            while 0 <= index <= len(gas_list) - 1:\n",
    "                gas = gas_list[index]\n",
    "                cost = cost_list[index]\n",
    "\n",
    "                gas_remain += gas\n",
    "\n",
    "                could_go_next = False\n",
    "                if gas_remain >= cost:\n",
    "                    could_go_next = True\n",
    "                if could_go_next: \n",
    "                    gas_remain -= cost\n",
    "                    if one_loop_finished and index >= start_index:\n",
    "                        no_way_out = False\n",
    "                        break\n",
    "                    if index == len(gas_list) - 1:\n",
    "                        one_loop_finished = True\n",
    "                        index = 0\n",
    "                        continue\n",
    "                else:\n",
    "                    no_way_out = True\n",
    "                    break\n",
    "                \n",
    "                index += 1\n",
    "            \n",
    "            return no_way_out == False\n",
    "\n",
    "        # print(car_travel(3))\n",
    "        # return 0\n",
    "\n",
    "        for i in range(0, len(gas)):\n",
    "            if car_travel(i):\n",
    "                return i\n",
    "        return -1\n",
    "    \n",
    "        #2022/12/15 06:56\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5aa91341-a3af-49bd-bea7-e6d113cb0908",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
